package cn.itsource.basic.service.impl;

import cn.itsource.basic.mapper.BaseMapper;
import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.IBaseService;
import cn.itsource.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;


    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }
    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }
    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.delete(id);
    }
    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    @Override
    public PageResult<T> queryPage(BaseQuery query) {
        //查询数据的总条数
        Long total=baseMapper.count(query);
        if (total==null||total==0){
            return new PageResult<>();
        }
        //查询当前页的数据
        List<T> list=baseMapper.page(query);

        return new PageResult<>(list,total);
    }

    @Override
    @Transactional
    public void allDelete(Long[] ids) {

        baseMapper.allDelete(ids);


    }
}
